<template>
  <div class="table-all">
  <!-- 搜索区 -->
  <div class="app-container search-box">
    <SearchForm
      :form="searchData.form"
      :options="searchData.options"
      @searchUpdate="search"
      @resetSearchData="resetSearchData"
    ></SearchForm>
  </div>
  <!-- 按钮区 -->
  <div class="app-container table-box">
    <div class="table-main">
      <!-- 表格 -->
      <v-table
        title="运行日报信息"
        v-if="tableMainHeight>0"
        :table-columns="tableColumns"
        :table-data="tableData"
        :loading="loading"
        :is-index="true"
        ref="table"
        :maxHeight="tableMainHeight">
        <template #topButtonList>
          <ButtonForm :buttonList="buttonList" @clickButton="clickButton"></ButtonForm>
        </template>
        <template #largeType="scope">
          {{getDictValue(scope.row.largeType, largeTypeOptions)}}
        </template>
        <template #smallType="scope">
          {{getDictValue(scope.row.smallType, smallTypeOptions)}}
        </template>
        <template #operation="scope">
          <a v-hasPermission="['compForeReport:download']" class="table-btn-edit table-operation"
             @click="handleDownload(scope.row)">{{$t('table.download')}}</a>
          <a v-hasPermission="['compForeReport:add']" class="table-btn-edit table-operation"
             @click="handleEdit(scope.row)">{{$t('table.edit')}}</a>
          <a v-hasPermission="['compForeReport:delete']" class="table-btn-delete table-operation"
             @click="singleDelete(scope.row)">{{$t('table.delete')}}</a>
        </template>
      </v-table>
    </div>
    <div class="table-page">
      <!-- 页码 -->
      <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                  @pagination="search"/>
    </div>
  </div>
  <!-- 详情 -->
  <detailDialog :dialog-visible="detailDialogShow"
                @success="editSuccess"
                @close="editClose"
                :openType="detailOpenType"
                :id="detailId"
                :title="detailTitle"
  ></detailDialog>
  </div>
</template>

<script>
  import { compForeReportPage,
    compForeReportExport,
    compForeReportDelete } from '@/api/alarm/foreReport'
  import SearchForm from '@/components/searchForm/index'
  import ButtonForm from '@/components/ButtonForm/index'
  import VTable from '@/components/VTable/index'
  import Pagination from '@/components/Pagination/index'
  import detailDialog from './foreReportUpload.vue'
  export default {
    components:{
      VTable,Pagination,detailDialog,SearchForm,ButtonForm
    },
    data(){
      return{
        tableColumns:[
          {
            prop: 'name',
            label: '报告名称',
            minWidth: '150',
            slotname: 'name',
            options: {}
          },
          {
            prop: 'largeType',
            label: '业务状态大类',
            minWidth: '150',
            slotname: 'largeType',
            options: {}
          },
          {
            prop: 'smallType',
            label: '业务专题细类',
            minWidth: '150',
            slotname: 'smallType',
            options: {}
          },
          {
            prop: 'unitName',
            label: '上传单位',
            minWidth: '150',
            slotname: 'unitName',
            options: {}
          },
          {
            prop: 'person',
            label: '上传人员',
            minWidth: '150',
            slotname: 'person',
            options: {}
          },
          {
            prop: 'uploadTime',
            label: '上传时间',
            minWidth: '150',
            slotname: 'uploadTime',
            options: {}
          },
          {
            prop: 'operation',
            label: '操作',
            width: '200',
            align: 'center',
            slotname: 'operation',
            overflow: false,
            fixed: 'right'
          }
        ],
        tableData:[],
        selection: [],
        loading:false,
        total:1,
        pageNum:1,
        pageSize:10,
        detailOpenType:"add",
        detailDialogShow:false,
        detailId:"",
        detailTitle:"新增",
        dict:{},
        searchData:{
          form:{
            name:'',
            largeType:'',
            smallType:'',
            unitName:'',
          },
          options:[
            {
              title:'报告名称',
              key: "name",
              type: "text",
            },
            {
              title:'业务状态大类',
              key: "largeType",
              type: "text",
            },
            {
              title:'业务专题细类',
              key: "smallType",
              type: "text",
            },
            {
              title:'上传单位',
              key: "unitName",
              type: "text",
            },
          ]
        },
        buttonList:[
          {name:'上传',methods:'handleAdd',type:'primary', hasPremission:["compForeReport:add"] },
          // {name:'导入',methods:'handleImport'},
          // {name:'导出',methods:'handleExport'}
        ],
        tableMainHeight: 0,
        // 字典
        largeTypeOptions:[],
        smallTypeOptions:[],
      }
    },
    watch: {
      // 字典
      'largeTypeOptions':{
        handler(newVal, oldVal) {
          this.searchData.options.forEach(t=>{
            if(t.key=='largeType'){
              t.option.data = newVal
            }
          })
        },
        deep: true,
        immediate: true
      },
      'smallTypeOptions':{
        handler(newVal, oldVal) {
          this.searchData.options.forEach(t=>{
            if(t.key=='smallType'){
              t.option.data = newVal
            }
          })
        },
        deep: true,
        immediate: true
      },
    },
    methods:{
      handleDownload(row){
          console.log("下载=====》row",row)
        if(row.fileUrl){
          let flieList = JSON.parse(row.fileUrl)
          flieList.forEach(t=>{
            // 遍历下载
            window.open(process.env.VUE_APP_BASE_API + 'file/api/download/'+t.fileId);
          })
        }
      },
      getCheckBoxValue(value,option){
        if(!value){
          return
        }
        let temp = []
        let data = value.split(",")
        data.forEach(t=>{
          let item = option.find(x=> x.value == t)
          if(item != undefined){
            temp.push(item.label)
          }
        })
        return temp.join(",")
      },
      getDictValue(value,option){
        let item = option.find(t=> t.value == value)
        if(item!=undefined){
          return item.label
        }
        return value
      },
      editClose(){
        this.detailDialogShow = false
      },
      editSuccess(){
        this.search()
        this.detailDialogShow = false
      },
      search(){
        this.fetch({
          ...this.searchData.form,
        })
      },
      fetch(params={}){
        params.pageSize = this.pageSize
        params.pageNum = this.pageNum
        params.countyCode = this.$store.state.account.user.deptIds
        this.loading = true
        compForeReportPage(params).then(res=>{
          if(res.code==200){
            this.total = res.data.total
            this.tableData = res.data.rows
          }else{
            this.$message({
              message: this.$t('tips.systemException'),
              type: 'error'
            })
          }
        }).finally(final=>{
          this.loading = false
        })
      },
      // 重置搜索条件
      resetSearchData(){
        this.searchData.form = {
          name:'',
          largeType:'',
          smallType:'',
          unitName:'',
        }
      },

      // 触发按钮区域按钮方法
      clickButton(methods){
        this[methods]()
      },
      handleAdd(){
        this.detailOpenType = "add"
        this.detailTitle = "新增"
        this.detailDialogShow = true
      },
      handleEdit(row){
        this.detailId = row.id
        this.detailOpenType = "edit"
        this.detailTitle="编辑"
        this.detailDialogShow = true
      },
      handleExport(){
        let params = {
          ...this.searchData.form
        }
        compForeReportExport(params,`预测报告管理_${new Date().getTime()}.xlsx`);
      },
      handleImport(){
        console.log("等待处理")
      },
      closeDialog(){
        this.detailDialogShow = false
      },

      singleDelete(row){
        this.$confirm(this.$t('tips.confirmDelete'), this.$t('common.tips'), {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning'
        }).then(() => {
          this.loading = true
          compForeReportDelete(row.id).then(res=>{
            if(res.code==200){
              this.$message({
                message: this.$t('tips.deleteSuccess'),
                type: 'success'
              })
              this.search()
            }else{
              this.$message({
                message: this.$t('tips.systemException'),
                type: 'error'
              })
            }
          }).finally(final=>{
            this.loading = false
          })
        })
      }
    },
    activated(){
      this.fetch()
    },
    created(){
      // 获取字典值
    },
    mounted(){
      this.tableMainHeight = document.getElementsByClassName('table-main')[0].clientHeight - 44 //减去顶部按钮列表的高度
    }
  }
</script>

<style lang="scss" scoped>
  .table-all {
    display: flex;
    flex-direction: column;
    height: 100%;

    .search-box {
      padding-bottom: 6px;
    }

    .table-box {
      padding-top: 10px;
      flex: 1;
      display: flex;
      flex-direction: column;

      .table-main {
        flex: 1;
        overflow: auto;
      }

      .table-page {
        // padding-top: 10 px;
        // padding-bottom: 10 px;
      }

      ::v-deep .pagination-container {
        text-align: right;
      }

    }
  }
</style>
